import * as os from 'os';
import { staticDir } from './storage-utils';

/// 获取的是内网地址  在服务器中不能使用
function getIpAddress(): string {
    const interfaces = os.networkInterfaces();

    for (const devName in interfaces) {
        const iface = interfaces[devName];
        for (let i = 0; i < iface.length; i++) {
            const alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }

    return "127.0.0.1";
}

export const PORT = 3000;
export const HOST = "192.168.99.8";
export const OLDHOSTURL = "http://192.168.99.8:3000";

export const pathToURL = (path: string): string => {
    let hostUrl = `http://${HOST}:${PORT}`;
    let url = path.replace(staticDir, hostUrl);
    return url.replace(/\\/g, '/');
}

export const urlToPath = (url: string): string => {
    let hostUrl = `http://${HOST}:${PORT}`;
    return url.replace(hostUrl, staticDir);
}

export const fixURL = (path: string): string => {
    let hostUrl = `http://${HOST}:${PORT}`;
    let url = path.replace(OLDHOSTURL, hostUrl);
    return url.replace(/\\/g, '/');
}